/*************************************************************************
	> File Name: tests/uart.c
	> Author: 
	> Mail: 
	> Created Time: 2020年12月15日 星期二 21时57分24秒
 ************************************************************************/

// #define UART_STATE (*(unsigned int*)0x00100008)
// #define UART_DATA  (*(unsigned int*)0x00100004)

#define UART_DATA  (*(unsigned int*)0x10000004)
#define UART_STATE (*(unsigned int*)0x10000008)

#define SW_BUTTON_REG (*(unsigned int*)0x10001000)

#define PWM_PERIOD_REG (*(unsigned int*)0x10002000)
#define PWM_DUTY_REG   (*(unsigned int*)0x10002004)

#define DIGITAL_CTRL_VAL (*(unsigned int*)0x10007000)
#define DIGITAL_CTRL_SEL (*(unsigned int*)0x10007004)

void _putc(unsigned char c)
{
	while(UART_STATE & 0x1);	
	UART_DATA = c;
}

void uart0_puts(char* str)
{
	while(*str != '\0') {
		_putc((unsigned char)(*str));
		str++;
	}
}

void button_release(unsigned int btn_vec)
{
	int btf ;//= SW_BUTTON_REG & 0x01;
	//Button Pressed Wait for release
	do{
		btf = SW_BUTTON_REG & btn_vec;
	}while(btf);
}

int main()
{	
	unsigned int sw_btn_map;
	unsigned char ureg;
	unsigned char dig_cnt;
	dig_cnt = 0;
	while(1) {

		if(SW_BUTTON_REG & 0x01) {
			uart0_puts("BUTTON 0 PUSHED");
			button_release(0x01);
		}

		if(SW_BUTTON_REG & 0x02) {
			uart0_puts("BUTTON 1 PUSHED");
			DIGITAL_CTRL_SEL = 0x01;
			DIGITAL_CTRL_VAL = dig_cnt ;
			dig_cnt = dig_cnt == 9 ? 0 : (dig_cnt + 1);
			button_release(0x02);
		}

		if (UART_STATE & 0x2) {
			ureg = UART_DATA; 
			if(ureg == 'H')
				uart0_puts("hello world 3-5");
		}
	}
	return 0;
}


